package functions.ems_claim;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import com.beans.AuditProcessBean;
import com.beans.FormBean;
import com.beans.TaskBean;
import com.crc.mam.BaseCase;
import coms.Constants;
import frame.HttpHelper;

/**
 * 提交报账单
 * @author CHENHAILIAN6
 *
 */
public class ClaimProcess extends BaseCase{
	private org.slf4j.Logger logger = LoggerFactory.getLogger(ClaimProcess.class);

	/**
	 * 提交报账申请
	 * submitProcessUrl="/ems-claim/claimProcess/submitProcess"
	 */
	public void submitProcess(String claimId,FormBean form) {
		logger.info("提交报账申请 -----------------start ------------");
		String params = "{\"formInstance\":{\"claimId\":" + claimId
				+ "},\"baseFlowRequest\":{\"title\":\"\",\"actionType\":\"start\",\"formId\":\"" + form.getFormId()
				+ "\",\"formInstId\":\"\",\"formInstanceId\":\"\",\"formsetId\":\"" + form.getFormSetId()
				+ "\",\"processInstanceId\":\"\",\"procDefKey\":\"" + form.getProcdefKey() + "\",\"procDefId\":\""
				+ form.getProcdefId()
				+ "\",\"actId\":\"\",\"comment\":\"\",\"fileIds\":\"\",\"submitType\":\"\",\"taskId\":\"\"}}";
		String url =Constants.SUBMIT_PROCESS_URL;
		try {
			result = HttpHelper.post(params, url);
			status = HttpHelper.isOk;
			if(status) {
			  logger.info("submitProcess result --- " + result);
			  JSONObject object = new JSONObject(result);
			  JSONObject request = object.getJSONObject("baseFlowRequest");
			  JSONObject variable = request.getJSONObject("variable");
			  
			}else {
			  try {
			    JSONObject object = new JSONObject(result);
			    remark = object.getString("message");
			  }catch(JSONException e) {
			    e.printStackTrace();
			    remark = "JSON解析错误";
			  }
			}

		} catch (Exception e) {
			e.printStackTrace();
			remark = "系统异常";
		}
		logger.info("提交报账申请 -----------------end ------------");
	}

	
	/**
	 * 提交审批
	 */
	public void adminsubmit(String claimId,TaskBean taskBean,AuditProcessBean processBean) {
		logger.info("提交审批 ------------------- start---------------------");
		String params = "{\"formInstance\":"
				+ "{\"claimId\":\""+claimId+"\"},"
				+ "\"baseFlowRequest\":"
				    + "{\"title\":\"\",\"actionType\":\"todo\",\"formId\":\""+processBean.getFormId()+"\","
				    + "\"formInstId\":\""+claimId+"\",\"formInstanceId\":\""+claimId+"\",\"formsetId\":\""+processBean.getFormSetId()+"\","
				    + "\"processInstanceId\":\""+taskBean.getProcesInstanceID()+"\",\"procDefKey\":\""+processBean.getPorcSetKey()+"\","
				    + "\"procDefId\":\""+processBean.getProcDefId()+"\",\"actId\":\""+taskBean.getActID()+"\",\"comment\":\"同意\","
				    + "\"fileIds\":\"\",\"submitType\":\"通过\",\"taskId\":\""+taskBean.getCurrentTaskID()+"\"}}";
		
		String url =  Constants.SUBMIT_PROCESS_URL;
    	try {
			result = HttpHelper.post( params, url);
			logger.info("submit --- " + result);
			JSONObject object = new JSONObject(result);
			JSONObject statusDatas = object.getJSONObject("status");
			String isSuccess = statusDatas.get("status").toString();
			if(isSuccess.trim().equals("success")) {
				status = true;
			}
    	}catch(Exception e) {
    		e.printStackTrace();
    	}
    //	logger.info("claimNo-----" + claimNo);
    	logger.info("提交审批 ------------------- end---------------------");
	}
}
